# This code is so you can run the samples without installing the package
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..'))

# stdlib
import copy
import random
import weakref

# pyglet related
import pyglet

# cocos2d related
from cocos.euclid import Point2
from cocos import sprite

# tetrico related
from constants import *
# from status import status
# import levels

__all__ = ['GameModel']

#
# Model (of the MVC pattern)
#

class GameModel( pyglet.event.EventDispatcher ):

    def __init__(self):
        super(GameModel,self).__init__()

        # self.init()

        # current block
        self.bicho = Pacman('p.png')

        # grid
        # self.map = {}

        # status.reset()


        # phony level
        # status.level = levels.levels[0]

    def set_controller( self, ctrl ):
        self.ctrl = weakref.ref( ctrl )


    def bicho_right( self ):
        '''moves right the bicho 1 square'''
        #self.block.backup()
        self.bicho.x += self.bicho.velx
        # if not self.is_valid_block():
        #     self.block.restore()
        # else:
        self.dispatch_event("on_move_bicho")

    def bicho_left( self ):
        '''moves left the block 1 square'''
        # self.block.backup()
        self.bicho.x -= self.bicho.velx
        # if not self.is_valid_block():
        #     self.block.restore()
        # else:
        self.dispatch_event("on_move_bicho")

    def bicho_down( self ):
        '''moves down the block 1 square'''
        # self.block.backup()
        self.bicho.y -= self.bicho.vely
        # if not self.is_valid_block():
        #     self.block.restore()
        #     self.next_block()
        # else:
        #     if sound:
        self.dispatch_event("on_move_bicho")

    # def bicho_stop( self ):
    #     '''stops the bicho'''
    #     while True:
    #         self.block.backup()
    #         self.bicho.y -= 1
    #         if not self.is_valid_block():
    #             self.block.restore()
    #             break
    #     self.dispatch_event("on_stop_bicho")

    def bicho_up( self ):
        '''moves down the block 1 square'''
        self.bicho.y += self.bicho.vely
        self.dispatch_event("on_move_bicho")


class Bicho( sprite.Sprite ):
    '''Base class for all blocks'''
    def __init__(self, image):
        super( Bicho, self).__init__(image)

    # def draw( self ):
    #     pass

class Pacman( Bicho ):
   def __init__(self, image):
        super( Pacman, self).__init__(image)
        self.x = WINDOW_WIDTH/2
        self.y = WINDOW_HEIGHT/2
        self.velx = 16
        self.vely = 16 

GameModel.register_event_type('on_move_bicho')
